package com.hnichr.ump.api.controller;

import com.hnichr.ump.common.ecard.entity.EcardBusinessCard;
import com.hnichr.ump.common.ecard.entity.EcardCompany;
import com.hnichr.ump.common.ecard.service.EcardBusinessCardService;
import com.hnichr.ump.common.ecard.service.EcardCompanyService;
import com.hnichr.ump.common.ecard.vo.BusinessCardVO;
import com.hnichr.ump.common.ecard.vo.WxBindVO;
import com.mxpioframework.common.vo.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "EcardController", description = "电子名片")
@RestController
@RequestMapping("/api/ecard/v1")
public class EcardController {

    @Autowired
    private EcardBusinessCardService ecardBusinessCardService;

    @Autowired
    private EcardCompanyService ecardCompanyService;

   /* @PostMapping("create/card")
    @Operation(summary = "新增card", description = "新增card", method = "POST")
    public Result<EcardBusinessCard> createCard(@RequestBody EcardBusinessCard card) {
        ecardBusinessCardService.save(card);
        return Result.OK();
    }*/

    @PutMapping("bind")
    @Operation(summary = "绑定微信openid", description = "绑定微信openid", method = "PUT")
    public Result<String> bind(@RequestBody WxBindVO bindVO) {
        String result = ecardBusinessCardService.bind(bindVO);
        if(StringUtils.isNotBlank(result)){
            return Result.error(result);
        }
        return Result.OK();
    }


    /*@PutMapping("update/card")
    @Operation(summary = "编辑card", description = "编辑card", method = "PUT")
    public Result<EcardBusinessCard> updateCard(@RequestBody EcardBusinessCard card) {
        ecardBusinessCardService.update(card);
        return Result.OK();
    }*/

    @GetMapping("fetch/card/{openId}")
    @Operation(summary = "查询card", description = "查询card", method = "GET")
    public Result<BusinessCardVO> fetchCard(@PathVariable(name = "openId", required = true) String openId) {
        EcardBusinessCard card = ecardBusinessCardService.getByOpenId(openId);
        if(card==null){
            return Result.error("未查到人员数据");
        }
        String companyId = card.getCompanyId();
        EcardCompany company = ecardCompanyService.getById(EcardCompany.class,companyId);
        if(company==null){
            return Result.error("未查到公司数据");
        }
        BusinessCardVO cardVO = new BusinessCardVO();
        BeanUtils.copyProperties(card,cardVO);
        cardVO.setLocation(company.getCompanyName());
        cardVO.setLogo(company.getLogo());
        cardVO.setUrl(company.getUrl());
        cardVO.setIntroduction(company.getIntroduction());
        return Result.OK(cardVO);
    }

    @GetMapping("fetch/openid/{code}")
    @Operation(summary = "换openid", description = "换openid", method = "GET")
    public Result<String> getOpenId(@PathVariable(name = "code", required = true) String code) {
        String openId = ecardBusinessCardService.getOpenIdByCode(code);
        if(StringUtils.isBlank(openId)){
            return Result.error("获取用户openid失败");
        }
        return Result.OK(openId);
    }


    @GetMapping("share/{openid}")
    @Operation(summary = "分享", description = "分享", method = "GET")
    public Result<String> share(@PathVariable(name = "openid", required = true) String openid) {
        EcardBusinessCard card = ecardBusinessCardService.getByOpenId(openid);
        if(card==null){
            return Result.error("找不到用户");
        }
        if(card.getShareCnt()==null){
            card.setShareCnt(1);
        }
        else{
            card.setShareCnt(card.getShareCnt()+1);
        }
        ecardBusinessCardService.update(card);
        return Result.OK();
    }


}
